package com.wuzhiming.istudy.controller;

import com.wuzhiming.istudy.dto.ResponseResult;
import com.wuzhiming.istudy.elasticsearch.CourseRespository;
import com.wuzhiming.istudy.pojo.User;
import com.wuzhiming.istudy.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: BaseController
 * @Description: 控制器的基类，所有控制器必须继承此类
 * @Author:zhimingw
 * @Version 1.0
 * @since 2022/2/27 13:37
 */
public class BaseController {

    @Autowired
    public CourseTypeService courseTypeService;

    @Autowired
    public CourseStageService courseStageService;

    @Autowired
    public CourseService courseService;

    @Autowired
    public UserService userService;

    @Autowired
    public CollectionService collectionService;

    @Autowired
    public CommentService commentService;

    @Autowired
    public NoteService noteService;

    @Autowired
    public StatisticsService statisticsService;

    @Autowired
    CourseRespository courseRespository;

    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;

    protected ResponseResult result;
    //当前登录用户信息
    protected User userInfo;
    //管理员登录信息
    protected User adminInfo;
    /**
     * 在每个子类方法调用之前先调用，设置request,response,session这三个对象
     */
    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request,HttpServletResponse response){
        this.request = request;
        this.response = response;
        this.session = request.getSession();
        result = new ResponseResult();
        //设置session中user对象
        userInfo = (User) session.getAttribute("user");
        //设置session中admin对象
        adminInfo = (User) session.getAttribute("admin");
    }
}
